﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mime;
using System.Web;
using System.Web.Services;
using System.Text;
using System.IO;
using MyAspx.Framework.WebUnit.CodeZipTree;
using MyAspx.Framework.Common;
using System.Collections.Specialized;
using MyAspx.EasyFramework.EntityFramework;
//using ;
namespace WebUI.Services.CodeTree
{
    /// <summary>
    /// Summary description for $codebehindclassname$
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class GetFileTree : IHttpHandler
    {
        commyaspxdbEntities _commyaspxdbEntities = new commyaspxdbEntities();
        public void ProcessRequest(HttpContext context)
        {
            //string dir = Explorer.GetCurrentPath(context);
            ////var basePath = context.Request.PhysicalApplicationPath;
            //dir = Microsoft.JScript.GlobalObject.unescape(dir);
            //System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(dir);
            ////dir = dir.ToNewPath();
            //dir = dir.ToNewPath();
            //StringBuilder sb = new StringBuilder(2000);
            //sb.Append("<ul class=\"jqueryFileTree\" style=\"display: none;\">\n");
            //foreach (System.IO.DirectoryInfo di_child in di.GetDirectories())
            //    sb.AppendFormat("\t<li class=\"directory collapsed\"><a href=\"#\" rel=\"{0}{1}\" relname=\"{2}\">{2}</a></li>\n", dir, di_child.Name.ToLower(),di_child.Name);
            //foreach (System.IO.FileInfo fi in di.GetFiles())
            //{
            //    string ext = "";
            //    if (fi.Extension.Length > 1)
            //        ext = fi.Extension.Substring(1).ToLower();

            //    sb.AppendFormat("\t<li class=\"file ext_{0}\"><a href=\"#\" rel=\"{1}{2}\"  relname=\"{3}\">{3}</a></li>\n", ext, dir, fi.Name.ToLower(), fi.Name);
            //}
            //sb.Append("</ul>");
            //context.Response.Write(sb.ToString());
            context.Response.ContentType = "text/plain";
            
            //获取当前源码的服务端路径

            string dir = Explorer.GetCurrentPath(context);

            dir = Microsoft.JScript.GlobalObject.unescape(dir);
          
            string a="";
            NameValueCollection b =  new NameValueCollection();
             UrlOper.ParseUrl(context.Request.UrlReferrer.AbsoluteUri, out a,out  b );
             string fileid = b.GetValues("guidstr").First().ToString();
             string filrname = _commyaspxdbEntities.my_downurl.First(q => q.downfileid == fileid).downurl;
             filrname = filrname.Split('/')[filrname.Split('/').Length - 1].Split('.')[0].ToString();
             if (dir.Split('/').Length <= 3) { dir = dir +""+ filrname+"/";  }
             else
             { dir = dir; }
            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(dir);
            dir = dir.ToNewPath();
            StringBuilder sb = new StringBuilder(2000);
            sb.Append("<ul class=\"jqueryFileTree\" style=\"display: none;\">\n");
            foreach (System.IO.DirectoryInfo di_child in di.GetDirectories())
                sb.AppendFormat("\t<li class=\"directory collapsed\"><a href=\"#codeinfodata\" rel=\"{0}{1}/\" relname=\"{2}\">{2}</a></li>\n", dir, di_child.Name.ToLower(), di_child.Name);
            foreach (System.IO.FileInfo fi in di.GetFiles())
            {
                string ext = "";
                if (fi.Extension.Length > 1)
                    ext = fi.Extension.Substring(1).ToLower();
                if(ext =="cs" || ext =="vb" ||ext =="asp" ||ext =="aspx" ||ext =="js" ||ext =="css" ||ext =="xml" ||ext =="txt" ||ext =="html" ||ext =="htm" ||ext =="config" ||ext =="sln" ||ext =="suo" ||ext =="ashx" || ext =="svf" ||ext =="csproj" ||ext =="css" ||ext =="less" ||ext =="amhx" ){

                    sb.AppendFormat("\t<li class=\"file ext_{0}\"><a onclick=\"window.open('../Form/CodeFileDisplay.aspx?fid={4}&urldata={1}{2}','_blank')\" href=\"#codeinfodata\" rel=\"{1}{2}\"  relname=\"{3}\">{3}</a></li>\n", ext, dir, fi.Name.ToLower(), fi.Name, fileid);
                }
                else
                {
                    sb.AppendFormat("\t<li class=\"file ext_{0}\"><a href=\"#codeinfodata\" rel=\"{1}{2}\"  relname=\"{3}\">{3}</a></li>\n", ext, dir, fi.Name.ToLower(), fi.Name, fileid);
                }
            }
            sb.Append("</ul>");
            context.Response.Write(sb.ToString());
        }

        public String getFileHtml(string ext, string path)
        {
            string shtml ="";
            using (StreamReader _newStreamReader = new StreamReader(path,Encoding.Default))
            {
                if ("cs,js,sln,csproj,vb,css,txt,sql,java,aspx,cshtml,ashx,config".Split(',').Count(find => find == path.Split('.')[2].ToLower()) > 0)
                {
                    shtml = _newStreamReader.ReadToEnd() ;
                }
            }
            return shtml;
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        //public unsafe string
    }
}
